package com.mdd.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IUserFriendGroupService;
import com.mdd.admin.validate.UserFriendGroupCreateValidate;
import com.mdd.admin.validate.UserFriendGroupUpdateValidate;
import com.mdd.admin.validate.UserFriendGroupSearchValidate;
import com.mdd.admin.vo.UserFriendGroupListedVo;
import com.mdd.admin.vo.UserFriendGroupDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.UserFriendGroup;
import com.mdd.common.mapper.UserFriendGroupMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 用户好友分组实现类
 * @author DEREK
 */
@Service
public class UserFriendGroupServiceImpl implements IUserFriendGroupService {
        
    @Resource
    UserFriendGroupMapper userFriendGroupMapper;

    /**
     * 用户好友分组列表
     *
     * @author DEREK
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<UserFriendGroupListedVo>
     */
    @Override
    public PageResult<UserFriendGroupListedVo> list(PageValidate pageValidate, UserFriendGroupSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<UserFriendGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        userFriendGroupMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:userId@user_id:int",
            "=:toUserId@to_user_id:str",
            "like:groupName@group_name:str",
            "=:type:int",
            "=:extend:str",
        });

        IPage<UserFriendGroup> iPage = userFriendGroupMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<UserFriendGroupListedVo> list = new LinkedList<>();
        for(UserFriendGroup item : iPage.getRecords()) {
            UserFriendGroupListedVo vo = new UserFriendGroupListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 用户好友分组详情
     *
     * @author DEREK
     * @param id 主键参数
     * @return UserFriendGroup
     */
    @Override
    public UserFriendGroupDetailVo detail(Integer id) {
        UserFriendGroup model = userFriendGroupMapper.selectOne(
                new QueryWrapper<UserFriendGroup>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        UserFriendGroupDetailVo vo = new UserFriendGroupDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 用户好友分组新增
     *
     * @author DEREK
     * @param createValidate 参数
     */
    @Override
    public void add(UserFriendGroupCreateValidate createValidate) {
        UserFriendGroup model = new UserFriendGroup();
        model.setUserId(createValidate.getUserId());
        model.setToUserId(createValidate.getToUserId());
        model.setGroupName(createValidate.getGroupName());
        model.setType(createValidate.getType());
        model.setExtend(createValidate.getExtend());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        userFriendGroupMapper.insert(model);
    }

    /**
     * 用户好友分组编辑
     *
     * @author DEREK
     * @param updateValidate 参数
     */
    @Override
    public void edit(UserFriendGroupUpdateValidate updateValidate) {
        UserFriendGroup model = userFriendGroupMapper.selectOne(
                new QueryWrapper<UserFriendGroup>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setUserId(updateValidate.getUserId());
        model.setToUserId(updateValidate.getToUserId());
        model.setGroupName(updateValidate.getGroupName());
        model.setType(updateValidate.getType());
        model.setExtend(updateValidate.getExtend());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        userFriendGroupMapper.updateById(model);
    }

    /**
     * 用户好友分组删除
     *
     * @author DEREK
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        UserFriendGroup model = userFriendGroupMapper.selectOne(
                new QueryWrapper<UserFriendGroup>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        userFriendGroupMapper.delete(new QueryWrapper<UserFriendGroup>().eq("id", id));
    }

}
